#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <string.h>

#define MAX_LENGTH 80
#define MAX_ROW 5

char *s_gets(char *st, int n) {
    char *ret_val;
    ret_val = fgets(st, n, stdin);
    if (ret_val) {
        while (*st != '\n' && *st != '\0') {
            ++st;
        }
        if (*st == '\n') {
            *st = '\0';
        } else
            while (getchar() != '\n')
                continue;
    }
    return ret_val;
}

//打印菜单
void menu();
//打印字符串
void show_strings(int m,int n,char sts[m][n]);

//复制字符串
void copy_strings(int m,int n,char target[m][n],char source[m][n]);
//通过字符串长度排序
void sort_st_by_length(int m,int n,char sts[m][n]);
void sort_st_by_ascii(int m,int n,char sts[m][n]);
void sort_st_by_fword(int m,int n,char sts[m][n]);
int length_of_rword(char st[]){
    int sum=0;
    while (!isspace(*st++)) sum++;
    return sum;
}
int main() {
    char sts[MAX_ROW][MAX_LENGTH];
    char opts[MAX_ROW][MAX_LENGTH];
    int i = 0;
    for (; i < MAX_ROW; ++i) {
        printf("输第%d句话（最多%d句）:", i + 1,MAX_ROW);
        char* val = s_gets(sts[i], MAX_LENGTH);
        if (!val) {
            puts("退出成功");
            return 0;
        }
    }
    copy_strings(i, MAX_LENGTH, opts, sts);
    int choice;
    do {

        menu();
        int t = scanf("%d", &choice);
        if (t == 1 && choice < 6 && choice > 0) {
            switch (choice) {
                case 1:
                    puts("未排序");
                    show_strings(i, MAX_LENGTH, sts);
                    break;
                case 2:
                    puts("以ascii排序后：");
                    sort_st_by_ascii(i, MAX_LENGTH, opts);
                    show_strings(i, MAX_LENGTH, opts);
                    break;
                case 3:
                    puts("以句长排序后：");
                    sort_st_by_length(i, MAX_LENGTH, opts);
                    show_strings(i, MAX_LENGTH, opts);
                    break;
                case 4:
                    puts("以首单词长排序后：");
                    sort_st_by_fword(i, MAX_LENGTH, opts);
                    show_strings(i, MAX_LENGTH, opts);
                    break;
                case 5:
                    puts("Bye!");
                    return 0;
                default:
                    break;
            }
        }
    } while (choice != 5);
    return 0;
}

void menu() {
    puts("**************************************");
    puts("请选择下面的操作：");
    puts("1.打印源字符列表");
    puts("2.以ascii的顺序打印字符串");
    puts("3.按长度递增打印字符列表");
    puts("4.按字符串中第一个单词长度的顺序打印字符串");
    puts("5.退出");
    puts("***************************************");
}

void show_strings(int m,int n,char sts[m][n]){
    for (int i = 0; i < m; ++i) {
        puts(sts[i]);
    }
}

void copy_strings(int m,int n,char target[m][n],char source[m][n]){
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            target[i][j] = source[i][j];
        }
    }
}
void sort_st_by_length(int m,int n,char sts[m][n]){
    for (int i = 0; i < m; ++i) {
        for (int j = i+1; j < m; ++j) {
            if (strlen(sts[j]) < strlen(sts[i])) {
                char temp[n];
                for (int k = 0; k < n; ++k) {
                    temp[k] = sts[i][k];

                }
                for (int k = 0; k < n; ++k) {
                    sts[i][k] = sts[j][k];
                }
                for (int k = 0; k < n; ++k) {
                    sts[j][k] = temp[k];
                }
            }
        }
    }
}

void sort_st_by_ascii(int m,int n,char sts[m][n]){
    for (int i = 0; i < m; ++i) {
        for (int j = i+1; j < m; ++j) {
            if (strncmp(sts[i],sts[j],MAX_LENGTH)>0) {
                char temp[n];
                for (int k = 0; k < n; ++k) {
                    temp[k] = sts[i][k];

                }
                for (int k = 0; k < n; ++k) {
                    sts[i][k] = sts[j][k];
                }
                for (int k = 0; k < n; ++k) {
                    sts[j][k] = temp[k];
                }
            }
        }
    }
}

void sort_st_by_fword(int m,int n,char sts[m][n]){
    for (int i = 0; i < m; ++i) {
        for (int j = i+1; j < m; ++j) {
            if (length_of_rword(sts[i]) > length_of_rword(sts[j])) {
                char temp[n];
                for (int k = 0; k < n; ++k) {
                    temp[k] = sts[i][k];

                }
                for (int k = 0; k < n; ++k) {
                    sts[i][k] = sts[j][k];
                }
                for (int k = 0; k < n; ++k) {
                    sts[j][k] = temp[k];
                }
            }
        }
    }
}